//
//  GoodsManageViewController.m
//  SameWorld(merchant)
//
//  Created by 李小朋 on 2017/3/29.
//  Copyright © 2017年 杨旭. All rights reserved.
//

#import "GoodsManageViewController.h"
#import "GoodsCell.h"
#import "GoodsManageViewModel.h"
#import "UserInfo.h"
@interface GoodsManageViewController ()<UITableViewDelegate ,UITableViewDataSource>
@property (nonatomic ,strong) UserInfo *userInfo;
@property (nonatomic ,strong) UITableView *tableView;
@property (nonatomic ,strong) NSMutableArray *goodsListArray;
@end

@implementation GoodsManageViewController


- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        _userInfo = [UserInfo sharedUserInfo];
    }
    return self;
}

- (NSMutableArray *)goodsListArray {
    if (!_goodsListArray) {
        self.goodsListArray = [NSMutableArray array];
    }
    return _goodsListArray;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.title = @"商品管理";
    [self loadDataGoodsList];
//    [self selectedViewUI];
    [self createTableViewUI];
//    [self.tableView.mj_header beginRefreshing];
    
}

- (void)loadDataGoodsList {

    // 商家编号
    NSString *bhStr =[NSString stringWithFormat:@"%@",_userInfo.bh];
    GoodsManageViewModel *goodsViewModel = [[GoodsManageViewModel alloc] init];
    [goodsViewModel loadGoodsListNetWorking:bhStr andReturnValue:^(id returnValue) {
        self.goodsListArray = returnValue;
        if (self.goodsListArray.count == 0) {
            UIImageView *backImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"什么都没有-1.jpg"]];
            backImage.frame = self.view.frame;
            [self.view addSubview:backImage];
        }
        [self.tableView reloadData];

        
    } andReturenError:^(id errorCode) {
        
    }];
}


- (void)createTableViewUI {

    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, size_width, size_height - 56) style:(UITableViewStylePlain)];
//    self.tableView.backgroundColor = [UIColor colorWithHexString:@"f2f2f2"];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    [self.view addSubview:self.tableView];
    UIView *headerView = [[UIView alloc] initWithFrame:(CGRectMake(0, 0, size_width, 8))];
    headerView.backgroundColor = [UIColor colorWithHexString:@"f2f2f2"];
    self.tableView.tableHeaderView = headerView;
    //注册cell
    [self.tableView registerNib:[UINib nibWithNibName:@"GoodsCell" bundle:nil] forCellReuseIdentifier:@"GoodsCell"];
}

- (void)selectedViewUI {

    UIView *selectedView = [[UIView alloc] initWithFrame:(CGRectMake(0, 0, size_width, 42))];
    selectedView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:selectedView];
    
    NSArray *titles = @[@"在线",@"待上线",@"已下线"];
    UISegmentedControl *segmented = [[UISegmentedControl alloc] initWithItems:titles];
    segmented.frame = CGRectMake((selectedView.frame.size.width - 296)/2, (selectedView.frame.size.height - 26)/2, 296, 26);
    segmented.tintColor = [UIColor orangeColor];
    segmented.selectedSegmentIndex = 0;
    [segmented addTarget:self action:@selector(segmentClicked:) forControlEvents:(UIControlEventValueChanged)];
    [selectedView addSubview:segmented];
}



#pragma mark - 选择按钮的方法
- (void)segmentClicked:(UISegmentedControl *)sender {
    NSInteger index = sender.selectedSegmentIndex;
    switch (index) {
        case 0 :
        {
            NSLog(@"在线");
        }
            break;
        case 1 :
        {
            NSLog(@"待上线");
        }
            break;
        case 2 :
        {
            NSLog(@"已下线");
        }
            break;
        default:
            break;
    }
}


#pragma mark - UITableView的代理方法
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.goodsListArray.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    GoodsCell *cell = [tableView dequeueReusableCellWithIdentifier:@"GoodsCell" forIndexPath:indexPath];

    [cell setCellUpDataWithGoodsManageModel:self.goodsListArray[indexPath.section]];

    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 74;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 8;
}

@end
